package com.examine.config;

import java.sql.SQLException;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.DataSource;

import com.examine.action.Login;
import com.examine.dao.UserData;
import com.examine.permission.PermissionInterceptor;
import com.google.inject.Binder;
import com.google.inject.Module;

/**
 * Module for Guice.
 */
public class GuiceModule implements Module {

	public void configure(Binder binder)  {
		binder.bind(Login.class).asEagerSingleton();
		binder.bind(PermissionInterceptor.class).asEagerSingleton();
		binder.bind(UserData.class).asEagerSingleton();

		try {
			bindDataSource(binder);
		} catch (NamingException e) {
			e.printStackTrace();
		}
	}

	protected void bindDataSource(Binder binder) throws NamingException {
		
		Context initialContext = new InitialContext();
		
		DataSource datasource = (DataSource) initialContext.lookup("java:comp/env/jdbc/mysql");

		binder.bind(DataSource.class).toInstance(datasource);
	}

}
